package com.dingchw.singlemusic.security;

import com.dingchw.singlemusic.define.exception.DataException;

public enum EncryptEnum {
    RSA("RSA", RSAEncrypt.class);
    private String algorithm;
    private Class<? extends IEncrypt> handler;

    EncryptEnum(String algorithm, Class<? extends IEncrypt> handler){
        this.algorithm = algorithm;
        this.handler = handler;
    }

    public IEncrypt getHandler(){
        try{
            return this.handler.newInstance();
        }catch (Exception e){
            throw new DataException("加密算法不存在");
        }
    }

    public IEncrypt getHandler(String algorithm){
        EncryptEnum targetEncryptEnum = null;
        algorithm = algorithm.toUpperCase();
        for(EncryptEnum encryptEnum : values()){
            if(algorithm.equals(encryptEnum.algorithm)){
                targetEncryptEnum = encryptEnum;
            }
        }
        if(null == targetEncryptEnum){
            throw new DataException("加密算法不存在");
        }
        try{
            return targetEncryptEnum.handler.newInstance();
        }catch (Exception e){
            throw new DataException("加密算法不存在");
        }
    }
}
